import { useHistory, useLocation } from "react-router-dom";

export default function Login() {
  const { search } = useLocation();
  const { replace } = useHistory();

  function handleLogin() {
    localStorage.setItem("token", "123");

    // 对 search 做处理
    const query = new URLSearchParams(search);

    // 获取 redirect 参数的值
    const redirect = query.get("redirect") || "/home";

    // 跳转
    replace(redirect);

    // 跳转 /home
    // push("/home");
  }

  return (
    <div>
      <h1>Login</h1>

      <button onClick={handleLogin}>点我，就登录成功</button>
    </div>
  );
}
